//
//  NSDate+CLExt.m
//  Rippton_iphone
//
//  Created by yeyongping on 2017/11/16.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "NSDate+CLExt.h"

@implementation NSDate (CLExt)
+ (NSDate *) dateWithYear:(NSInteger)year
                    month:(NSInteger)month
                      day:(NSInteger)day
                     hour:(NSInteger)hour
                   minute:(NSInteger)minute
                   second:(NSInteger)second{
//    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *systemTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setCalendar:gregorian];
    [dateComps setYear:year];
    [dateComps setMonth:month];
    [dateComps setDay:day];
    [dateComps setTimeZone:systemTimeZone];
    [dateComps setHour:hour];
    [dateComps setMinute:minute];
    [dateComps setSecond:second];
    return [dateComps date];
}
/**
 
 时间戳转data
 */
+ (NSDate *)dataWithTimeSp:(long)timeSp{
    NSTimeInterval interval = timeSp / 1000;
    return [NSDate dateWithTimeIntervalSince1970:interval];
}
/**
 data转为时间戳
 
 */
- (long)timeSp{
    return (long)[[NSNumber numberWithDouble:[self timeIntervalSince1970]] integerValue];
}


/**
 date 转为年月日时分秒
 */
- (NSString *)getTimeWithFormatter:(NSString *)formatterStr{
    // 默认
    if (formatterStr.length == 0) {
        formatterStr = @"yyyy-MM-dd HH:mm:ss";
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatterStr];
    NSString *dateString  = [formatter stringFromDate:self];
    return dateString;
}



/*
 时间戳转时间 格式为 月-日-年 at 时间  例如：Jun 18th，2017 at 18：00
 */
- (NSString *)toRipptonDateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"HH:mm";
    [dateFormatter setLocale:[NSLocale currentLocale]];
    NSString *temStr = [dateFormatter stringFromDate:self];
    
    NSArray *monthArr = @[@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec"];
    NSInteger index = [self getMonth];
    NSString *monthString = monthArr[index -1];
    NSString *dayString = [NSString stringWithFormat:@"%ld",(long)[self getDay]];
    NSString *yearString = [NSString stringWithFormat:@"%ld",(long)[self getYear]];
    
    return [NSString stringWithFormat:@"%@. %@, %@ %@",monthString,dayString,yearString,temStr];

}

/*
 时间戳转时间 格式为 月-日 at 时间  例如：Jun 18 at 18：00
 */
- (NSString *)toRipptonMonthDateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"HH:mm";
    [dateFormatter setLocale:[NSLocale currentLocale]];
    NSString *temStr = [dateFormatter stringFromDate:self];
    
    NSArray *monthArr = @[@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec"];
    NSInteger index = [self getMonth];
    NSString *monthString = monthArr[index -1];
    NSString *dayString = [NSString stringWithFormat:@".%ld",(long)[self getDay]];
    
    return [NSString stringWithFormat:@"%@ %@, %@",monthString,dayString,temStr];
}




/*
 时间戳转时间 格式为 月-日 年  例如：Jun 18th,2017 主要用于活动中
 */
- (NSString *)toRipptonYearAndMonthDateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"HH:mm";
//    [dateFormatter setLocale:[NSLocale currentLocale]];
//    NSString *temStr = [dateFormatter stringFromDate:self];
    
    NSArray *monthArr = @[@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec"];
    NSInteger index = [self getMonth];
    NSString *monthString = monthArr[index -1];
    NSString *dayString = [NSString stringWithFormat:@"%ld",(long)[self getDay]];
    NSString *yearString = [NSString stringWithFormat:@"%ld",(long)[self getYear]];
    
    return [NSString stringWithFormat:@"%@ %@, %@",monthString,dayString,yearString];
}

- (NSString *)toRipptonDateWithCreateDate:(long)createDate andNowDate:(long)nowDate{
    
   NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    NSCalendar *calendar = [NSCalendar calendar];
    NSTimeInterval interval = createDate / 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSString *timeStr = [date getTimeWithFormatter:@"yyyy:MM:dd:HH"];
    
    fmt.dateFormat = @"yyyy:MM:dd:HH";
    NSDate *createdAtDate = [fmt dateFromString:timeStr];
    
    if (createdAtDate.isThisYear) { // 今年
        if (createdAtDate.isToday) { // 今天
            // 手机当前时间
            NSTimeInterval currentInterval = nowDate / 1000;
            NSDate *currentDate = [NSDate dateWithTimeIntervalSince1970:currentInterval];
            NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
            NSDateComponents *cmps = [calendar components:unit fromDate:createdAtDate toDate:currentDate options:0];
            
            if (cmps.hour >= 1) { // 时间间隔 >= 1小时
                return [NSString stringWithFormat:@"%zd Hrs", cmps.hour];
            }else{
                return [NSString stringWithFormat:@"%zd Mins", cmps.minute];
            }
           
        }
        else { // 其他
          return  [date toRipptonMonthDateString];
        }
    } else { // 非今年
       return [date toRipptonDateString];
    }
    return nil;
}


- (NSInteger)getDay {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:self] day];
}

- (NSInteger)GetWeekday {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:self] weekday];
}

- (NSInteger)getMonth {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:self] month];
}

- (NSInteger)getYear {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:self] year];
}

- (NSString *)getCurrentLocationSp{
    return @"";
}
@end
